package com.zzxz.practicaltrainingsys.service.impl;

import com.zzxz.practicaltrainingsys.mapper.GradeMapper;
import com.zzxz.practicaltrainingsys.mapper.GroupVoMapper;
import com.zzxz.practicaltrainingsys.service.GroupVoService;
import com.zzxz.practicaltrainingsys.vo.GroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class GroupVoServiceImpl implements GroupVoService {
    @Autowired
    private GroupVoMapper groupVoMapper;

    @Override
    public List<GroupVo> getGroupsByTeacherAndProject(Integer teacherId, Integer projectId) {
        // 1. 获取基本组队信息（包含教师权限验证）
        List<GroupVo> groups = groupVoMapper.getGroupsByProjectIdAndTeacherId(projectId, teacherId);

        if (groups.isEmpty()) {
            return groups; // 如果没有数据，直接返回空列表
        }

        // 2. 为每个组添加成员信息
        return groups.stream().peek(group -> {
            // 处理副组长ID为null的情况
            Integer viceLeaderId = group.getViceLeaderId() != null ? group.getViceLeaderId() : -1;

            // 获取成员(排除组长和副组长)
            List<GroupVo.MemberVo> members = groupVoMapper.getTeamMembers(
                    group.getTeamId(),
                    group.getLeaderId(),
                    viceLeaderId
            );

            // 设置成员信息和数量
            group.setMembers(members);
            group.setMemberCount(members.size());
        }).collect(Collectors.toList());
    }
}

